<template>
  <div class="setting">
    <el-card>
      <el-tabs>
        <el-tab-pane label="角色管理">
          <el-button
            type="primary"
            size="small"
            style="margin: 30px 0 20px 0"
            @click="add"
          >+ 新增角色</el-button>
          <el-table :data="tableData" style="width: 100%" border>
            <el-table-column width="50" label="序号">
              <template v-slot="{ $index }">
                <div>{{ (page.page - 1) * page.pagesize + $index + 1 }}</div>
              </template>
            </el-table-column>
            <el-table-column
              label="角色名称"
              sortable
              prop="name"
              width="200px"
            />
            <el-table-column label="描述" sortable prop="description" />
            <el-table-column label="操作" width="300px">
              <template v-slot="{ row }">
                <div>
                  <el-button type="text" @click="operate(row.id)">操作权限</el-button>
                  <el-button type="text" @click="edit(row)">修改</el-button>
                  <el-button type="text" @click="del(row.id)">删除</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="公司信息" lazy> <info /> </el-tab-pane>
      </el-tabs>
      <div style="text-align: right; margin-top: 15px">
        <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          :page-sizes="[1, 5, 10, 20, 30, 40]"
          :current-page="page.page"
          :page-size="page.pagesize"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
      <!-- 新增弹框组件 -->
      <add ref="add" @getData="getData" />
      <!-- 分配权限组件 -->
      <SetPermission ref="SetPermission" />
    </el-card>
  </div>
</template>

<script>
import { sysRole, sysRoleDElete } from '@/api/setting'
// 使用cookie数据持久化
import jsCookie from 'js-cookie'
// 导入新增组件
import add from './components/add.vue'
import info from './components/info.vue'
import SetPermission from './components/SetPermission.vue'
export default {
  components: {
    add,
    info,
    SetPermission
  },
  data() {
    return {
      tableData: [],
      page: {
        page: 1,
        pagesize: +jsCookie.get('setting-cookie') || 1,
        total: 10
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 页容量发生变化的时候
    handleSizeChange(val) {
      this.page.pagesize = val
      // 存储页容量的值 到cookie
      jsCookie.set('setting-cookie', val)
      this.getData()
    },
    handleCurrentChange(val) {
      this.page.page = val
      this.getData()
    },
    // 调用接口api获取所有信息数据
    async getData() {
      const res = await sysRole(this.page)
      this.page.total = res.data.total
      this.tableData = res.data.rows
      // console.log(res)
    },
    // 打开弹框组件
    add() {
      this.$refs.add.isShow = true
      this.$refs.add.mock = 'add' // 打开弹框组件
    },
    del(id) {
      // console.log(row)
      this.$confirm('确认删除该员工', '提示', {
        type: 'warning'
      }).then(async() => {
        await sysRoleDElete(id)
        this.$message.success('删除员工成功')
        // 如果页码>1 且只有一项数据的时候 需要返回到前一页
        if (this.page.page > 1 && this.tableData.length <= 1) {
          this.page.page--
        }
        this.getData()
      })
    },
    // 编辑事件
    edit(row) {
      this.$refs.add.isShow = true // 打开弹框组件
      this.$refs.add.mock = 'edit' // 判断是新增还是编辑
      this.$refs.add.form = JSON.parse(JSON.stringify(row)) // 数据回显到表单内
    },
    // 打开操作组件
    operate(id) {
      this.$refs.SetPermission.isShow = true
      this.$refs.SetPermission.getData()
      this.$refs.SetPermission.getList(id)
    }
  }
}
</script>

<style lang="scss" scoped>
.setting {
  padding: 15px;
}
</style>
